package cn.zucc.edu.bruce.iot;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.Socket;

import cn.zucc.edu.bruce.iot.receiver.ReceiveIOTData;

public class dealSocket extends Thread implements Serializable {
    public static final int HEADER = 0xfffffffe;
    public static final int END = 0xeffffffe;
    Socket socket;
    InputStream is;
    OutputStream os;
    private ScrollingActivity activity;

    public dealSocket(Socket s, Context activity) {
        Log.e("start", "dealSocket: ");
        this.socket = s;
        this.activity = (ScrollingActivity) activity;
    }

    @Override
    public void run() {
        deal();
    }

    private void deal() {
        Log.e("start thread", "deal: ");
        try {
            is = socket.getInputStream();
            os = socket.getOutputStream();
            int len = 0;
            while (SocketServer.isContinue) {
                byte[] data = new byte[71];
                byte[] bytes = new byte[4];
                len += is.read(bytes);
                System.arraycopy(bytes, 0, data, 0, 4);
                int header = DataDeal.bytesToInt(bytes, 0);
                if (header == HEADER) {
                    ReceiveIOTData receiveIOTData = new ReceiveIOTData();
                    bytes = new byte[8];
                    len += is.read(bytes);
                    System.arraycopy(bytes, 0, data, 4, 8);
                    receiveIOTData.setSno(bytes);

                    bytes = new byte[2];
                    len += is.read(bytes);
                    System.arraycopy(bytes, 0, data, 12, 2);
                    receiveIOTData.setHeartbeat(bytes);

                    bytes = new byte[1];
                    len += is.read(bytes);
                    System.arraycopy(bytes, 0, data, 14, 1);
                    receiveIOTData.setIotId(bytes[0]);

                    bytes = new byte[4];
                    len += is.read(bytes);
                    System.arraycopy(bytes, 0, data, 15, 4);
                    receiveIOTData.setSensorNum(bytes);

                    for (int i = 0; i < 4; i++) {
                        bytes = new byte[13];
                        len += is.read(bytes);
                        System.arraycopy(bytes, 0, data, 19 + i * 13, 13);
                        receiveIOTData.addSensor(bytes);
                    }
                    byte checkSum = DataDeal.Check(data);

                    bytes = new byte[1];
                    len += is.read(bytes);
                    byte receiveCheckSum = bytes[0];
                    Log.e("checksum", "calculated: " + checkSum + "received checksum: " + receiveCheckSum);
                    receiveIOTData.setChecksum(bytes);

                    bytes = new byte[4];
                    len += is.read(bytes);
                    receiveIOTData.setEnd(bytes);
                    if (receiveIOTData.getEnd() == END && checkSum == receiveCheckSum) {
                        Message message = new Message();
                        message.what = 1;
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("receive", receiveIOTData);
                        bundle.putSerializable("send", dealSocket.this);
                        message.setData(bundle);
                        activity.myHandler.sendMessage(message);
                        if (activity.sendIOTData != null) {
                            byte[] bytes1=activity.sendIOTData.getBytes();
                            Log.e("send checksum", ""+bytes1[51] );
                            os.write(bytes1);
                            os.flush();
                        }
                    }
                }
                len = 0;
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            try {
                is.close();
                socket.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
